import {
    Component,
    OnInit,
    Injector,
    ViewChild,
    Output,
    EventEmitter,
    ElementRef,
    ViewEncapsulation
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    ListDtoExtensionsItem,
    ComboboxItemDto,
    StockZT_IntoBillItemServiceProxy,
    Stock_IntoToolItemForReturnListDto,
    StockZT_RoomServiceProxy
} from '@shared/service-proxies/stock-service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { LazyLoadEvent } from 'primeng/api';
import { Paginator } from 'primeng/primeng';
import { Table } from 'primeng/table';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import * as _ from 'lodash';

@Component({
    selector: 'MarketIntoBillItemModal',
    templateUrl: './market-into-billitem.component.html',
    animations: [appModuleAnimation()],
    encapsulation: ViewEncapsulation.None
})
export class MarketIntoBillImteModalComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private _billService: StockZT_IntoBillItemServiceProxy,
        private _RoomServiceProxy : StockZT_RoomServiceProxy
    ) {
        super(injector);
    }

    @ViewChild('createOrEditModal') modal: ModalDirective;
    @ViewChild('createOrEditModal') modalEl: ElementRef;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
  
    selectedDepts = new ComboboxItemDto();
    selectedValue: Array<Stock_IntoToolItemForReturnListDto>;
    extensions: Array<ListDtoExtensionsItem>;
    intoToolItemDto:Stock_IntoToolItemForReturnListDto;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Output() qccard: EventEmitter<any> = new EventEmitter<any>();

    // active = false;
    saving = false;
    closed = false;
    isShown = false;
    selectedWareHouse = new Array<ComboboxItemDto>();
    selectedPurchase = new Array<ComboboxItemDto>();
    roomOption;
    supplierFilter;//供货单位
    f_MaterialCodeFiter;//物料编码
    f_MaterialNameFiter;//物料名称
    purchaseType;
    ngOnInit(): void {       
        this._RoomServiceProxy.getLevelOneRoomListByStockType('市采物资库').subscribe(result2 => {
            this.roomOption = result2.items.map(e => {
                return {
                    label: e.displayText,
                    value: e
                };
            });
            this.selectedWareHouse = result2.items;
        });
    }
    show(purchaseType:number): void {
        //  this.active = true;
        this.purchaseType = purchaseType;
        this.closed = false;
    
        this.modal.show();
    }

    shown(): void {
        this.isShown = true;
        this.getRecordsIfNeeds(null);
    }

    getRecordsIfNeeds(event?: LazyLoadEvent): void {
        if (!this.isShown) {
            return;
        }

        this.getMarketIntoItems(event);
    }
    close(): void {
        //  this.active = false;
        this.modal.hide();
    }

    onHidden(event) {
        this.closed = true;
    }

    save() {
        this.modalSave.emit(this.selectedValue);
        this.selectedValue = null;
        this.close();
    }
    getMarketIntoItems(event?: LazyLoadEvent) {
        // if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
        //     this.paginator.changePage(0);

        //     return;
        // }
        this.primengTableHelper.showLoadingIndicator();

        this._billService
            .getIntoMarketItemListForReturnExtension(
                this.supplierFilter,
                this.f_MaterialCodeFiter,
                this.f_MaterialNameFiter,
                this.purchaseType,
                this.selectedWareHouse,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.extensions = result.items;
                this.primengTableHelper.totalRecordsCount = result.stock_IntoMarketItemList.totalCount;
                this.primengTableHelper.records = result.stock_IntoMarketItemList.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
}
